package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Question;
import com.tanhua.dubbo.mapper.QuestionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class QuestionApiImpl implements QuestionApi {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Question findQuestion(Long loginUserId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUserId);
        return questionMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveQuestion(Question question) {
        Question questionUser = findQuestion(question.getUserId());
        if (null != questionUser) {
            question.setId(questionUser.getId());
            questionMapper.updateById(question);
        } else {
            questionMapper.insert(question);
        }
    }
}
